<template>
  <div class="background">
    <el-card class="login-card">
      <h1 class="login-title">公租房租赁平台</h1>
      <el-form :model="loginForm" ref="loginForm" :rules="rules" label-width="0">
        <el-form-item prop="account">
          <el-input v-model="loginForm.account" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="密码"></el-input>
        </el-form-item>
        <el-form-item class="button-group">
          <el-button type="primary" @click="handleLogin">登录</el-button>
          <el-button @click="goToRegister">注册</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      loginForm: {
        account: "",
        password: ""
      },
      rules: {
        account: [{ required: true, message: "请输入用户名", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.$axios
            .post("/tenant/login", this.loginForm)
            .then(response => {
              if (response.data.code === 200) {
                this.$message.success("登录成功");
                this.$store.commit("setIsLogin", true);
                this.$store.commit("setLoginTenant", response.data.data[0]);
                this.$router.push(
                  "/",
                  () => {},
                  () => {}
                );
              } else {
                this.$message.error(response.data.message);
              }
            })
            .catch(error => {
              this.$message.error(error.message);
            });
        } else {
          this.$message.error("请输入用户名和密码");
          return false;
        }
      });
    },
    goToRegister() {
      this.$router.push(
        "/register",
        () => {},
        () => {}
      );
    }
  }
};
</script>

<style scoped>
@keyframes backgroundChange {
  0% {
    background-image: url("@/assets/image1.png");
  }
  33% {
    background-image: url("@/assets/image2.jpg");
  }
  66% {
    background-image: url("@/assets/image3.jpeg");
  }
  100% {
    background-image: url("@/assets/image1.png");
  }
}

.background {
  height: 100vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  animation: backgroundChange 30s infinite;
  background-size: cover;
  background-position: center;
}

.login-card {
  width: 400px;
  padding: 20px;
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(10px);
  border-radius: 10px;
  padding: 20px;
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #409eff;
}

.button-group {
  display: flex;
  justify-content: space-between;
}
</style>
